<?php
namespace Api\Controller;

use Think\Controller;

class UserController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 用户信息
     * @return [type] [description]
     */
    public function index()
    {

    }

    /**
     * 用户信息
     * @return [type] [description]
     */
    public function info()
    {
        $info = D('Api/User', 'Service')->info(USER_ID);
        success($info);
    }

    /**
     * 注册
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function register()
    {
        $mobile   = I('post.mobile', '', 'trim');
        $code     = I('post.code', '', 'trim');
        $password = I('post.password', '', 'trim');

        if (!is_phone($mobile)) {
            error(8002, '手机号码格式错误');
        }

        if (S('sms_' . $mobile) !== $code) {
            error(8003, '验证码错误');
        }

        if (!preg_match("/^[A-Za-z0-9]{6,18}$/", $password)) {
            error(8004, '密码只能是6-18位数字或大小写组成');
        }

        $user_id = D('Api/User', 'Service')->register($mobile, $password);

        if ($user_id) {
            success();
        } else {
            error(8005, '注册失败，用户已存在');
        }
    }

    /**
     * 登录
     * @return [type] [description]
     */
    public function login()
    {
        $mobile   = I('post.mobile', '', 'trim');
        $password = I('post.password', '', 'trim');

        if (!is_phone($mobile)) {
            error(8002, '手机号码格式错误');
        }

        $user_id = D('Api/User', 'Service')->login($mobile, $password, $ext = [
            'version'     => VERSION,
            'device_type' => DEVICE,
            'uuid'        => UUID,
        ]);

        if ($user_id) {
            success([
                'token' => encrypt_token($user_id),
                'info'  => D('Api/User', 'Service')->info($user_id),
            ]);
        } else {
            error(8007, '登录失败');
        }
    }

    /**
     * 忘记密码
     * @return [type] [description]
     */
    public function forgetPassword()
    {
        $mobile   = I('post.mobile', '', 'trim');
        $code     = I('post.code', '', 'trim');
        $password = I('post.password', '', 'trim');

        if (!is_phone($mobile)) {
            error(8002, '手机号码格式错误');
        }

        if (S('sms_' . $mobile) !== $code) {
            error(8003, '验证码错误');
        }

        if (!preg_match("/^[A-Za-z0-9]{6,18}$/", $password)) {
            error(8004, '密码只能是6-18位数字或大小写组成');
        }

        $is_update = D('Api/User', 'Service')->resetPassword($mobile, $password);

        if ($is_update) {
            success();
        } else {
            error(8006, '修改密码失败');
        }
    }

    /**
     * 重置密码
     * @return [type] [description]
     */
    public function resetPassword()
    {
        $mobile       = I('post.mobile', '', 'trim');
        $password     = I('post.password', '', 'trim');
        $new_password = I('post.new_password', '', 'trim');

        if (!is_phone($mobile)) {
            error(8002, '手机号码格式错误');
        }

        if (!preg_match("/^[A-Za-z0-9]{6,18}$/", $new_password)) {
            error(8004, '密码只能是6-18位数字或大小写组成');
        }

        $is_update = D('Api/User', 'Service')->resetPassword($mobile, $new_password, $password);

        if ($is_update) {
            success();
        } else {
            error(8006, '修改密码失败');
        }
    }

    /**
     * 检测用户是否存在
     * @return boolean [description]
     */
    public function isExists()
    {
        $mobile = I('post.mobile', '', 'trim');
        if (!is_phone($mobile)) {
            error(8002, '手机号码格式错误');
        }

        $res = D('Api/User', 'Service')->isExists($mobile);
        success(['isExists' => $res]);
    }

    /**
     * 注销
     * @param string $value [description]
     */
    public function logout()
    {
        $token = I('post.token', '', 'trim');
        S($token, null);

        success();
    }

    /**
     * 更新用户信息
     * @return [type] [description]
     */
    public function updateInfo()
    {
        $username = I('post.username', '', 'trim');
        $sex      = I('post.sex', 0, 'intval');
        $birth    = I('post.birth', '', 'trim');
        $province = I('post.province', '', 'trim');
        $city     = I('post.city', '', 'trim');

        $data = [];
        if ($username) {
            $data['username'] = $username;
        }

        if ($sex) {
            $data['sex'] = $sex;
        }

        if ($birth) {
            if (!is_date($birth)) {
                error(8025, '生日有误');
            }
            $data['birth'] = $birth;
        }

        if ($province) {
            $data['province'] = $province;
        }

        if ($city) {
            $data['city'] = $city;
        }

        $res = upload('User_Avatar');
        if (isset($res['avatar'])) {
            $data['avatar'] = C('DOMAIN') . $res['avatar']['url'];
        }

        D('Api/User', 'Service')->updateInfo(USER_ID, $data);

        success();
    }

    /**
     * 用户余额明细
     * @return [type] [description]
     */
    public function moneyLog()
    {
        $page = I('post.page', 0, 'intval');
        $log  = D('Api/User', 'Service')->moneyLog(USER_ID);

        success($log);
    }

    /**
     * 帮助中心url
     * @return [type] [description]
     */
    public function help()
    {
        $url = 'https://www.shanfengmao.com';
        success($url);
    }

    /**
     * 用户im信息
     * @return [type] [description]
     */
    public function imInfo()
    {
        # code...
    }
}
